package com.xzt.meeting.filter;

import com.xzt.meeting.service.UserService;
import com.xzt.meeting.utils.MeetingUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author xzt
 * @Date 2022/5/8 22:05
 * @Version 1.0
 * @Introduce
 */

@WebFilter("/*")
public class LoginFilter implements Filter {

    private UserService userService = new UserService();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // 统一处理乱码
        request.setCharacterEncoding("UTF-8");


        String uri = request.getRequestURI();

        String targetName = uri.substring(uri.lastIndexOf("/") + 1);

        if (
                uri.startsWith("/bootstrap") ||
                        uri.startsWith("/css") ||
                        uri.startsWith("/fonts") ||
                        uri.startsWith("/img") ||
                        uri.startsWith("/js") ||
                        uri.startsWith("/upload") ||
                        uri.startsWith("/vendor") ||
                        targetName.equals("checkCode.jsp") ||
                        targetName.equals("forget.jsp") ||
                        targetName.equals("index.jsp") ||
                        targetName.equals("taglib.jsp") ||
                        targetName.equals("register.jsp") ||
                        targetName.equals("login") ||
                        targetName.equals("logout") ||
                        targetName.equals("register") ||
                        targetName.equals("sendEmail") ||
                        targetName.equals("checkUsername") ||
                        targetName.equals("sendCode") ||
                        targetName.equals("checkEmail") ||
                        targetName.equals("forgetPassword") ||
                        targetName.equals("wx_login")
        ) {

            //放行资源
            filterChain.doFilter(request, response);


            return;
        }


        HttpSession session = request.getSession();
        if (session.getAttribute("loginUser") != null) {

            filterChain.doFilter(request, response);

            return;
        }


        Cookie loginCookie = MeetingUtils.getCookie(request, "loginUser");

        if (loginCookie != null) {

            Long userId = Long.parseLong(loginCookie.getValue());

            session.setAttribute("loginUser", userService.findUserById(userId));
            filterChain.doFilter(request, response);

            return;
        }

        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    @Override
    public void destroy() {

    }
}
